#!/usr/bin/env python
# $Id: phennywrapper.py 72 2009-11-20 15:54:20Z michon1992 $
#
# This file is part of Phennui.
#
# Phennui is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Phennui is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Phennui.  If not, see <http://www.gnu.org/licenses/>.

try:
  import vte
except:
  vte = False
  
import pygtk
pygtk.require('2.0')
import gtk, gobject
import os

class phennywrapper:
    
    def __init__(self, args):
        self.term = vte.Terminal()
        self.term.connect("child-exited", lambda term: gtk.main_quit())
        self.term.fork_command()
        self.term.feed_child('cd %s\n' % os.path.dirname(args[0]))
        self.term.feed_child('python %s\n' % ' '.join(args))
        self.term.set_sensitive(False)

        self.window = gtk.Window()
        self.window.add(self.term)
        self.window.connect('delete-event', lambda window, event: gtk.main_quit())
        self.window.show_all()
        self.window.set_deletable(False)
        
        gobject.io_add_watch(sys.stdin, gobject.IO_IN, self.interpreter)

    def interpreter(self, stdin, flag):
        line = stdin.readline()[:-1]
        if not line:
            return True
        
        if line == 'show':
            self.window.present()
        elif line == 'hide':
            self.window.hide()
        elif line == 'quit':
            exit(0)
    
        return True
        
    def main(self):
        gtk.main()
        

if __name__ == '__main__':
    import sys
    if vte:
        pw = phennywrapper(sys.argv[1:])
        pw.main()

